package net.caixiaomi.info.adapter;

import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.model.ElementCellModel;

/**
 * 足球玩法选择框内容适配器
 */

public class ElementCellAdapter extends BaseQuickAdapter<ElementCellModel, BaseViewHolder> {
    public ElementCellAdapter(@LayoutRes int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, ElementCellModel item) {
        try {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.append(item.getCellName());
            String cellOdds = item.getCellOdds();
            if (!TextUtils.isEmpty(cellOdds)) {
                stringBuilder.append("\n").append(item.getCellOdds());
            }

            TextView textView = helper.getView(R.id.text);

            textView.setBackgroundResource(R.drawable.bg_orange_select);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) textView.getLayoutParams();
            layoutParams.topMargin = (int) CommonApp.getInstance().getResources().getDimension(R.dimen.divider);
            layoutParams.rightMargin = (int) CommonApp.getInstance().getResources().getDimension(R.dimen.divider);
            SpannableString spannableString = new SpannableString(stringBuilder);
            if (!TextUtils.isEmpty(cellOdds)) {
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), item.isSelected() ? R.color.white : R.color.third_text)), stringBuilder.length() - item.getCellOdds().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new RelativeSizeSpan(0.8f), stringBuilder.length() - item.getCellOdds().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            textView.setText(spannableString);
            textView.setTextColor(ContextCompat.getColor(CommonApp.getInstance(), item.isSelected() ? R.color.white : R.color.primary_text));
            textView.setSelected(item.isSelected());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
